//
//  SPUserInfoController.m
//  Sport
//
//  Created by Cass on 2018/3/28.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPUserInfoController.h"
#import "SPUserInfo.h"
#import "SPConversationViewController.h"
@interface SPUserInfoController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIImageView *headImg;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *bgImg;
@property (weak, nonatomic) IBOutlet UILabel *persignature;

@property (strong, nonatomic) SPUserInfo *userInfo;
@end

@implementation SPUserInfoController


- (IBAction)sendMessage:(id)sender {
    SPConversationViewController *conversationVC = [[SPConversationViewController alloc]initWithConversationChatter:_userInfo.accountName conversationType:EMConversationTypeChat];
    conversationVC.title = _userInfo.nickname;
    conversationVC.nickname =  _userInfo.nickname;;
    conversationVC.iconUrl = _userInfo.headimage;
    [self.navigationController pushViewController:conversationVC animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.headImg.layer.cornerRadius = self.headImg.bounds.size.width / 2;
    self.headImg.layer.masksToBounds = YES;
}


- (void)setIndex:(NSNumber *)index{
    NSString *ID = [NSString stringWithFormat:@"%@",index];
    NSLog(@"ID号:%@",ID);
    NSString *url = [NSString stringWithFormat:@"http://39.108.132.194/Sport/returnUserInfo.php?id=%@",ID];
    [SPHttpClient POST:url parameters:nil success:^(id responseObject) {
        NSLog(@"--接受个人信息成功--%@",responseObject);
        NSArray *array = responseObject;
        for (NSDictionary *dict in array) {
            SPUserInfo *userInfo = [SPUserInfo mj_objectWithKeyValues:dict];
            self.userInfo = userInfo;
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"--接受个人信息失败--%@",error);
    }];
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFJSONResponseSerializer serializer];
//     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",@"text/json",@"text/javascript",@"text/plain", nil];
//    [manager POST:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"--访问成功--%@",responseObject);
//        NSArray *array = responseObject;
//        for (NSDictionary *dict in array) {
//            SPUserInfo *userInfo = [SPUserInfo mj_objectWithKeyValues:dict];
//            self.userInfo = userInfo;
//        }
//        [self.tableView reloadData];
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"--访问失败--%@",error);
//    }];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }else{
        return 3;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:self.userInfo.headimage] placeholderImage:[UIImage imageNamed:@"ha"]];
    self.persignature.text = self.userInfo.persignature;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = [NSString stringWithFormat:@"昵称:     %@",self.userInfo.nickname];
        }else if (indexPath.row == 1){
            cell.textLabel.text = [NSString stringWithFormat:@"性别:     %@",self.userInfo.gender];
        }else if (indexPath.row == 2){
            cell.textLabel.text = [NSString stringWithFormat:@"年龄:     %@",self.userInfo.age];
        }
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            cell.textLabel.text = [NSString stringWithFormat:@"爱好:     %@",self.userInfo.hobby];
        }else if (indexPath.row == 1){
            cell.textLabel.text = [NSString stringWithFormat:@"所在地:     %@",self.userInfo.location];
        }else{
            cell.textLabel.text = [NSString stringWithFormat:@"职业:     %@",self.userInfo.occupation];
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5.0f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

@end
